package service.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import db.MyHibernateSessionFactory;
import entity.Students;
import service.StudentsDAO;

public class StudentsDAOImpl implements StudentsDAO{

	@Override
	public List<Students> queryAllStudetns() {
		Transaction tx = null;
		List<Students> list = null;
		String hql = "";
		
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			hql = "from Students";
			tx = session.beginTransaction();
			Query query = session.createQuery(hql);
			list = query.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(tx != null){
				tx = null;
			}
		}
		
		return list;
	}

	@Override
	public Students queryStudentsBySid(String sid) {
		Transaction tx = null;
		Students student = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			student = (Students)session.get(Students.class, sid);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(tx != null){
				tx = null;
			}
		}
		return student;
	}

	@Override
	public boolean addStudents(Students student) {
		boolean flag = false;
		student.setSid(this.getNewSid());
		Transaction tx = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.save(student);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(tx != null){
				tx = null;
			}
		}
		return flag;
	}

	@Override
	public boolean updateStudents(Students studens) {
		boolean flag = false;
		Transaction tx = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.update(studens);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(tx != null){
				tx = null;
			}
		}
		return flag;
	}

	@Override
	public boolean deleteStudents(String sid) {
		boolean flag = false;
		Transaction tx = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			Students s = (Students)session.get(Students.class, sid);
			session.delete(s);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
		}finally{
			if(tx != null){
				tx = null;
			}
		}
		
		return flag;
	}
	
	private String getNewSid(){
		Transaction tx = null;
		String hql = "";
		String sid = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			hql = "select max(sid) from Students";
			Query query = session.createQuery(hql);
			sid = (String)query.uniqueResult();
			if(sid == null || sid.equals("")){
				sid = "s0000001";
			}else{
				String temp = sid.substring(1);
				int i = Integer.parseInt(temp);
				i++;
				temp = String.valueOf(i);
				int len = temp.length();
				for(int j = 0;j < 7-len; j++){
					temp = "0" + temp;
				}
				tx.commit();
				sid = "s" + temp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(tx != null){
				tx = null;
			}
		}
		
		return sid;
	}
}
